﻿<?php
require_once("function.php");
judgeLogin();
error_reporting(0);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!--<meta http-equiv="X-UA-Compatible" content="IE=7" /><!--如果是IE8时强制以IE7显示-->
<meta name="keywords" content="食品笔记,食品,食品安全,饮食,饮食安全,生活"><!--SEO关键字-->
<meta name="description" content="食品笔记，你的饮食安全卫士"><!--SEO描述-->
<link rel="shortcut icon" type="image/ico" href="images/favicon.ico"><!--选项卡图标-->
<title>食品笔记 - 让生活更健康</title>
<link rel="stylesheet" href="css/base.css">
<link rel="stylesheet" href="css/common.css">
<link rel="stylesheet" href="css/style.css">
<!--[if ie]>
<link rel="stylesheet" href="css/ie.css">
<script type="text/javascript" src="js/DD_belatedPNG.js" ></script>
<script type="text/javascript">
DD_belatedPNG.fix('.png');/*在需要png透明的元素里面加上.png样式即可（class="png"）*/
</script>
<![endif]-->
</head>
<body>
	<div class="header w pr"><!-- header -->
		<div class="index-header w960 bc pr">
			<a href="home.php" class="score_logo score_img dib png fl mt10"></a>
			<div class="header_right fr pr">
				<ul class="header_nav fl tc fb f12 h">
					<li><a href="home.php">首 页</a></li>
					<li><a href="report.php" class="header_nav_hover">饮食报告</a></li>
					<li><a href="newsmap.php">食品新闻</a></li>
					<li><a href="food.php">选食专栏</a></li>
					<li><a href="timeline.php">时光轴</a></li>
					<li><a href="share.php">分享</a></li>
				</ul>
				<a href="massage.php" class="header_myMessage tc fr ml5" title="私信"><?php echo showCountMassage($_SESSION['login_mail']);?></a><!-- 私信 -->
				<div class="header_myInfo fr f14 h pr">
					<a href="#" class="header_setup"><img src="<?php if(!empty($_SESSION['qq_nickname'])): echo $_SESSION['qq_photoUrl']; else: echo showUserPhoto();endif;?>" alt="" class="fl" /></a><!-- user_avatar -->
					<a href="#" class="fl header_setup"><?php echo showUserName();?></a><!-- user_nickname -->
					<div class="header_myInfoArrow score_img fl"></div>
					<ul class="header_hideInfo none pa tc w"><!-- hide_info -->
						<li><a href="#" class="header_setup">个人设置</a></li>
						<li><a href="modifymail.php">修改邮箱</a></li>
						<li><a href="password.php">修改密码</a></li>
						<li><a href="login_out.php">退出登录</a></li>
					</ul>
				</div>	
			</div>
			<div class="score_img content_top pa"></div>
		</div>
	</div><!-- end_header -->
	
	<div class="score_main pr oh h"><!-- main -->
		<div class="main w960 bc h">
			<div class="score_sider fl mt20">
				<div class="main_avatar">
					<img src="<?php if(!empty($_SESSION['qq_nickname'])): echo $_SESSION['qq_photoUrl']; else: echo showUserPhoto();endif;?>" alt="" class="score_avater mb20" /><!-- main_avatar -->
					<p><a href="#" class="score_nickname f18 fb header_setup"><?php echo showUserName();?></a> | <a href="massage.php" class="sider_message">私信(<?php echo showCountMassage($_SESSION['login_mail']);?>)</a></p><!-- main_nickname -->
				</div>
			</div>
			<?php
			//统计数据获取-添加剂
			//上限值 体重*adi
			$user_mail = $_SESSION['login_mail'];
			
			$username = getUserNick($user_mail);
			
			$weight = getUserWeight($username);
			
			$foodNameLimit = getUserEatFoodName($username);
			//print_r($foodNameLimit);
			
			$LimitData = array();
	
			foreach($foodNameLimit as $key=>$foodName)
			{
				$limitData[] = getLimitValue($weight,$foodName);
			}
			
			foreach($limitData as $keyLimit=>$valueLimit)
			{
				foreach($valueLimit as $keyLimit1=>$valueLimit1)
				{
					$limit[$keyLimit1] = $valueLimit1;
				}
			}
			//print_r($limit);
			
			
			//unset($LimitData);
			//print_r($limit);
			//echo "<br />";


			//实际值 净含量*最大使用量
			
			//返回净含量和食品名称
			$actualNetcontent = getActualNetcontent($username);
			//print_r($actualNetcontent); 
			
			//返回食品名称和添加剂值
			$actualData = array();
			
			foreach($actualNetcontent as $foodNameValue=>$netcontent)
			{
				$actualData[] = getActualValue($foodNameValue,$netcontent);
			}
			
			//print_r($actualData); 
			//echo "<hr />";
			
			//键名相同的值相加 rebuild a array
			$actualHandle = rebuild_array($actualData);
			
			//删除值为空的数组元素
			foreach($actualHandle as $k=>$v)
			{
				if($v == '')
				{
					unset($actualHandle[$k]);
				}else
				{
					$actual[$k] = $v;
				}
			}
			
			//unset($actualData);
			//print_r($actual); 
			//echo "<br />";

			//处理添加剂超标量
			$countData = array();
		
			foreach($limit as $key=>$value)
			{
				foreach($actual as $k=>$v)
				{	//比较同一种添加剂的上限值和实际值
					if($key == $k)
					{	//实际值>上限值，求出超出额
						if($v > $value)
						{//echo $key,$value;echo $k,$v;
							$countData[$key] = $v - $value;	

						}//end if $v > $value
						else
						{
							$countData[$key] = 0;	
						}
					}//end if $key = $k
				}//end foreach $actual
			}//end foreach $limit
				//Print_r($countData);
		//unset($actual);
		foreach($actual as $kk=>$vv)
		{
			foreach($countData as $kk1=>$vv1)
			{
				if($kk == $kk1)
				{
					$handleChaoBiao[$kk] = $vv1;
				}
			}
		}
		//Print_r($handleChaoBiao);
		//echo "<br />";
			
			//用户得分
			$userId = getUserId($_SESSION['login_mail']);
			$userHistoryScore = getUserHistoryScore($userId);
			
			//print_r($userHistoryScore);
			
			//echo $userHistoryScore[0];

			//各类食物的比重
			$categoryRateHandle = getEatedFoodCategoryRate($userId);

			//键名相同的值相加 rebuild a array
			$categoryRateEmpty = rebuild_arra($categoryRateHandle);
			//print_r($categoryRateEmpty);echo "<hr>";
			
			//删除值为空的数组元素
			$categoryRate = delArrayRepeat($categoryRateEmpty);
			//print_r($categoryRate);
			//总重量$sum
			(float)$sum = 0.0;
			foreach($categoryRate as $key=>$value)
			{
				$sum = $sum + $value;
			}
			//echo $sum;
			
			//计算各种食品种类所占的比重
			foreach($categoryRate as $key1=>$value1)
			{
				$value2 = $value1 / $sum;
				
				$value3 = round($value2, 3);
				
				$value4 = $value3 * 100;
				
				$rate[] = array($key1,$value4);
			}
			
			//print_r($userHistoryScore);
			//print_r($rate);
			
			
			//处理图表数据并输出，在script.js中用Jquery获取
				$biao1=showBiao($rate);
				$biao2=changeArray($actual);
				$biao2=showBiao($biao2);
				$biao2_1=changeArray($handleChaoBiao);
				$biao2_1=showBiao($biao2_1);
				$baio3=changeArray($userHistoryScore);
				$biao3=showBiao($baio3);
				//echo $biao3[0];
				
				//print_r($biao1);
				?>
				
			
			
			<div class="content fr">
				<div class="report_foodType bc">
					<h3 class="report_title f22">食品类型</h3>
					<?php if($biao1[0]!=""){?>
					<div id="report_foodTypePie" class="bc mb50 mt20"></div>
					<?php }else{
						//没有用户数据时输出
						echo "<div id='report_foodTypePie' class='bc mb50 mt20' style='display:none'></div>";
						echo "<div class=\"report_noData mt50 tc f20\">Sorry ˇ_ˇ ！你今天还没有饮食记录！</div>";
						}?>
					<hr class="news_hr mt20" />
				</div>
				<div class="report_additive mt30 bc">
					<h3 class="report_title f22">添加剂报告</h3>
					<?php if($biao2[0]!=""){?>
					<div id="report_additiveChart" class="bc mb50 mt20"></div>
					<?php }else{
						//没有用户数据时输出
						echo "<div id='report_additiveChart' class='bc mb50 mt20' style='display:none'></div>";
						echo "<div class=\"report_noData tc f20\">Sorry ˇ_ˇ ！你今天还没有添加剂记录！</div>";
						}?>
					<hr class="news_hr mt20" />
				</div>
				<div class="report_recentScores mt30 bc pb50">
					<h3 class="report_title f22">近日得分情况</h3>
					<?php if($biao3[0]!=""){?>
					<div id="report_coreLine" class="bc mb50 mt20"></div>
					<?php }else{
						//没有用户数据时输出
						//echo "<div id='report_coreLine' class='bc mb50 mt20' style='display:none'></div>";
						echo "<div class=\"report_noData mt50 tc f20\">Sorry ˇ_ˇ ！你还没有得分记录！</div>";
						}
					?>
				</div>
			</div>
		</div>
	</div><!-- end_main -->

	<div class="footer w"><!-- footer -->
		<div class="w960 bc pr">
			<div class="content_footer score_img pa"></div>
			<div class="pt30 welcome-footer bc">
				<span class="iphone-app-img dib png fl"></span><a href="#" class="iphone-app f12 a3 fl">iphone应用</a><a href="foodnote.apk" class="android-app f12 a3 fr">android应用</a><span class="android-app-img dib png fr"></span>
			</div>
			<div class="welcome-about mb10">
				<p class="f12 tc"><a href="#" class="a3">关于我们</a><span class="ml10 mr10">|</span><a href="#" class="a3">使用条款</a><span class="ml10 mr10">|</span><a href="#" class="a3">常见问题</a></p>
			</div>
			<div class="welcome-about f12 tc">
				<p class="a3 f12 c91">Copyright &copy; 2011-2012 Foodnote&nbsp;&nbsp;&nbsp;&nbsp; 湘ICP备09004495号-5</p>
			</div>
		</div>
	</div><!-- end_footer -->
	
		<?php
		//判断是否新用户登录
		$login_mail = $_SESSION['login_mail'];
		$login_status = judgeNewUser($login_mail);
		
		//获取用户数据
		$userData = getUserBasicInfo($_SESSION['login_mail']);
		
		if($login_status == 0)
		{
			echo "<div class='novice_login png pa w'>";
		}else
		{
			echo "<div class='novice_login png pa w none'>";
		}
	?>
		<div class="personal_info bc oh pb50 pr">
			<div class="personal_close score_img pa"></div>
			<h2 class="personal_title ml50 mt20 mb20">个人信息录入</h2>
			
			<form action="home.php" method="post" class="ml50">
				<fieldset class="fl">
					<p class="mt10"><label for="" class="personal_label f22">性别</label></p>
					<p class="mt10"><small class="personal_small f14">请选择：男/女</small></p>
					<p class="mt10">
						<select class="score_text score_select pl5 pr5 " name="sex">
					<?php  
						if($userData['user_sex']=="男"):
						
							echo "<option value='男' selected >男</option>";
							echo "<option value='女'>女</option>";
						
						elseif($userData['user_sex']=="女"):
						
							echo "<option value='男'>男</option>";
							echo "<option value='女' selected>女</option>";
						
						else:
						
							echo "<option value='' selected>--</option>";
							echo "<option value='男'>男</option>";
							echo "<option value='女'>女</option>";
						endif;
					?>
								
						</select>
					</p>
					<p class="mt10"><label for="" class="personal_label f22">年龄</label></p>
					<p class="mt10"><small class="personal_small f14">不同的年龄对饮食的需求不同，例如:23</small></p>
					<p class="mt10"><input type="text" class="score_text zy_ageVal pl5 pr5" name="age" value="<?php echo $userData['user_age'];?>"/></p>
					<p class="mt10"><label for="" class="personal_label f22">现居地</label></p>
					<p class="mt10"><small class="personal_small f14">请输入您现居地的省份名，例如:湖南</small></p>
					<p class="mt10">
						<select class="score_text score_select pl5 pr5 " name="city">
							<option value='<?php echo $userData['user_city'];?>' selected><?php echo $userData['user_city'];?></option>
							<option value='安徽'>安徽</option>
							<option value='福建'>福建</option>
							<option value='港澳台藏'>港澳台藏</option>
							<option value='甘肃'>甘肃</option>
							<option value='广东'>广东</option>
							<option value='广西'>广西</option>
							<option value='贵州'>贵州</option>
							<option value='海南'>海南</option>
							<option value='河北'>河北</option>
							<option value='黑龙江'>黑龙江</option>
							<option value='河南'>河南</option>
							<option value='湖北'>湖北</option>
							<option value='湖南'>湖南</option>
							<option value='江苏'>江苏</option>
							<option value='江西'>江西</option>
							<option value='吉林'>吉林</option>
							<option value='辽宁'>辽宁</option>
							<option value='内蒙古'>内蒙古</option>
							<option value='宁夏'>宁夏</option>
							<option value='青海'>青海</option>
							<option value='山东'>山东</option>
							<option value='山西'>山西</option>
							<option value='陕西'>陕西</option>
							<option value='四川'>四川</option>
							<option value='云南'>云南</option>
							<option value='新疆'>新疆</option>
							<option value='浙江'>浙江</option>
							<option value='直辖市'>直辖市</option>
						</select>
					</p>
					<p class="mt10"><label for="" class="personal_label f22">体重</label></p>
					<p class="mt10"><small class="personal_small f14">不同的体重对饮食的需求不同，单位：kg</small></p>
					<p class="mt10"><input type="text" class="score_text zy_weightVal pl5 pr5" name="weight" value="<?php echo $userData['weight'];?>"/> kg</p>
					<input type="hidden" value="<?php echo basename($_SERVER['PHP_SELF']);?>" name="fileName" />
					<input type="hidden" value="true" name="submited" />
					<input type="submit" value="确认提交" name="submit" class="personal_submit pa f18" />
				</fieldset>
			</form>
			<form method="post" action ="upload.php" enctype="multipart/form-data" target="hidden_frame" class="none">
				<fieldset>
					<input type="file" name="photo" id="photo" onchange="cli('p_submit');"/>
					<input type="submit" name="p_submit" value="保存" id="p_submit">
					<iframe name='hidden_frame' id="hidden_frame" style="border:none;" onreadystatechange="reflesh('<?php echo $_SESSION[login_mail]?>');"></iframe>
				</fieldset>
			</form>
			<form action="home.php" method="post" class="ml50">
				<fieldset>
					<a href="javascript:void(0)" onclick="cli('photo');"><img id="img" src="<?php if(!empty($_SESSION['qq_nickname'])): echo $_SESSION['qq_photoUrl']; else: echo showUserPhoto();endif;?>" class="personal_avatar pa" />
					<span class="pa click_upload">+点击上传头像</span>
					</a>
					<a href="javascript:void(0)" class="pa avatar_refresh" onclick="reflesh('<?php echo $_SESSION[login_mail]?>');">刷新</a>
				</fieldset>
			</form>
		</div>
	</div>
<!-- JavaScript -->
<script type="text/javascript" src="js/jquery-1.6.min.js"></script>
<script type="text/javascript" src="js/highcharts.js"></script>
<script type="text/javascript" src="js/exporting.js"></script>
<script type="text/javascript" src="js/script.js"></script>
<script type="text/javascript">  
 
 function cli(id){  
		var sub = document.getElementById(id);
   sub.click();  
//   if (/msie/i.test(navigator.userAgent)) //IE  
//   {  
//      sub.fireEvent("onclick");  
//   } else {  
//      var e = document.createEvent('MouseEvent');  
//      e.initEvent('click', false, false);  
//      sub.dispatchEvent(e);  
//   }
}



function reflesh(user){
	image_src="image.php?user_mail="+user+"&math="+Math.random(1);
	//alert(image_src);
	document.getElementById("img").src=image_src;
	
	}
</script> 	


<div class="b1_1" style="display:none"><?php echo $biao1[0];?></div>
<div class="b1_2" style="display:none"><?php echo $biao1[1];?></div>
<div class="b2_1" style="display:none"><?php echo $biao2[0];?></div>
<div class="b2_2" style="display:none"><?php echo $biao2[1];?></div>
<div class="b2_3" style="display:none"><?php echo $biao2_1[1];?></div>
<div class="b3_1" style="display:none"><?php echo $biao3[0];?></div>
<div class="b3_2" style="display:none"><?php echo $biao3[1];?></div>
<div class="none">
<script type="text/javascript">
var _bdhmProtocol = (("https:" == document.location.protocol) ? " https://" : " http://");
document.write(unescape("%3Cscript src='" + _bdhmProtocol + "hm.baidu.com/h.js%3Fbba169f4f4508eb5cc1c5618eeb3c59d' type='text/javascript'%3E%3C/script%3E"));
</script>
</div>			
</body>

</html>
<?php
function showBiao($array){
	for($i=0;$i<1000;$i++){
		if($array[$i][0]=='')break;
		if($i==0){
			$name=$array[0][0];
			$value=$array[0][1];
		}else{
			$name.="|".$array[$i][0];
			$value.="|".$array[$i][1];
		}
		
	}
	$temp[0]=$name;
	$temp[1]=$value;
	return $temp;
}



?>